// 导入数据库
const db = require('../db/index')
// 导入moment修改日期格式
const moment = require('moment')

// 获取产品类别库存总价图数据 无 =>
exports.getProductCategoryAndPrice = (req, res) => {
    // 获取产品类别数组函数
    const getCategoryArr = () => {
        return new Promise(resolve => {
            const sql1 = "select set_value from setting where set_name = '产品设置'"
            db.query(sql1, (err, result) => {
                const arr = eval('(' + result[0].set_value + ')')//eval() JSON格式字符串 转换为 JSON对象
                resolve(arr) // 返回对象类型的类别数组
            })
        })
    }
    // 获取单个类别的总价函数
    const getSingleTotalPrice = product_category => {
        return new Promise(resolve => {
            const sql2 = "select product_all_price from product where product_category = ?"
            db.query(sql2, product_category, (err, result) => {
                const sum = result.reduce((prev, current) => prev + current.product_all_price, 0)// 计算传入类别的总价
                resolve(sum)// 返回总价
            })
        })
    }
    // 返回数据函数
    async function getAll() {
        // 返回类别数组
        const categoryArr = await getCategoryArr()
        // 返回总价数组
        let priceArr = []
        for (let i = 0; i < categoryArr.length; i++) {
            priceArr[i] = await getSingleTotalPrice(categoryArr[i])
        }
        res.send({
            categoryArr,
            priceArr
        })
    }
    // 调用函数
    getAll()
}

// 获取管理员与用户对比图数据 无 =>
exports.getAdminCompareUser = (req, res) => {
    const sql1 = "select count(*) as value, `identity` as name from users group by `identity`"
    db.query(sql1, (err, result) => {
        if (err) return res.cc(err)
        res.send(result)
    })
}

// 获取公告等级分布图数据 无 =>
exports.getMessageLevelCompare = (req, res) => {
    const sql1 = "select count(*) as value, message_level as name from message where message_category = '公司公告' group by message_level"
    db.query(sql1, (err, result) => {
        if (err) return res.cc(err)
        res.send(result)
    })
}

// 获取每日登录人数图数据 无 =>
exports.getLoginNumberEveryDay = (req, res) => {
    // 获取七天日期数组
    const getDayArr = () => {
        let day = new Date()
        let week = []
        for (let i = 0; i < 8; i++) {
            day.toLocaleDateString().replace(/\//g, '-')// 日期格式转换：2024/12/21 => 2024-12-21 ,g代表全局匹配
            week.push(moment(day, 'YYYY-MM-DD').format('YYYY-MM-DD'))// 补零
            day.setDate(day.getDate() - 1)
        }
        return week.reverse()
    }
    // 根据日期获得每天登录的人数
    const getNumber = login_time => {
        return new Promise(resolve => {
            const sql1 = `select count(*) as number from login_log where login_time like '%${login_time}%'`
            db.query(sql1, (err, result) => {
                resolve(result[0].number)
            })
        })
    }
    // 返回数据函数
    async function getAll() {
        // 返回日期数组
        const dayArr = await getDayArr()
        // 返回登录人数数组
        let numberArr = []
        for (let i = 0; i < dayArr.length; i++) {
            numberArr[i] = await getNumber(dayArr[i])
        }
        res.send({
            dayArr,
            numberArr
        })
    }
    // 调用函数
    getAll()
}